package br.pucrio.inf.lac.konoha.hyuga.command;

import java.util.Properties;

import com.espertech.esper.client.EPServiceProvider;

import br.pucrio.inf.lac.konoha.hyuga.core.HyugaService;

public class CommandFactory {
	/** Properties. */
    private Properties properties;
    
	/** Hyuga. */
	private HyugaService hyuga;
	
	/** CEP Engine. */
	private EPServiceProvider cepEngine;
	
    public CommandFactory() {}

	public CommandFactory(Properties properties, HyugaService hyuga) {
	    this.properties = properties;
	    this.hyuga = hyuga;
	    this.cepEngine = hyuga.getCEPEngine();
	}
	
	public Command getCommand(String commandSTR) {
		String command = commandSTR.toUpperCase();

	    if (command.contains("DEBUG")) {
	        return new DebugCommand(commandSTR);
        }if (command.contains("LIST")) {
			return new ListCommand(commandSTR, cepEngine);
		} else if (command.contains("LOG")) {
            return new LogCommand(commandSTR, properties, cepEngine);
        } else if (command.contains("PLAY")) {
			return new PlayCommand(commandSTR, cepEngine);
        } else if (command.contains("WEB PARTITION")) {
            return new WebPartitionCommand(commandSTR, hyuga);
        } else if (command.contains("RESET")) {
            return new ResetCommand(commandSTR, cepEngine);
        } else if (command.contains("SEARCH")) {
            return new SearchCommand(commandSTR, cepEngine);
        } else {
			return new UnkownCommand(commandSTR, cepEngine);
		}
	}
	
	public String toString() {
	    StringBuffer sb  = new StringBuffer();
	    String formatSTR = "\t%-8s %-12s %-30s\n"; 
	    
	    sb.append("\nAVAILABLE COMMANDS:\n");
	    sb.append(String.format(formatSTR, "LIST", "size",  "List all context partitions with SIZE or higher moving objects."));
	    sb.append(String.format(formatSTR, "LOG", "period", "Log all clusters detected / dispersed up to period (in ms)."));
	    sb.append(String.format(formatSTR, "PLAY", "filename", "Replay (restream) filename.csv."));
	    sb.append(String.format(formatSTR, "RESET", "", "Reset the output base time."));
	    sb.append(String.format(formatSTR, "WEBGRID", "", "Send/Update grid indexes to webserver."));
	    sb.append(String.format(formatSTR, "DEBUG", "", "Placeholder command for debugging."));
	    
	    return sb.toString();
	}

}
